package com.apotik.legenda.frameinternal;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.MatteBorder;

import java.awt.SystemColor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JSeparator;
import javax.swing.border.LineBorder;
import javax.swing.JTextField;

import com.apotik.legenda.dao.UserDao;
import com.apotik.legenda.model.User;
import com.apotik.legenda.observer.IndexObserver;

public class LoginInternalFrame extends JInternalFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	int widthThis = 400;
	int heightThis = 250;
	
	private JTextField usernameField;
	private JPasswordField passField;
	private JButton btnSubmit;
	
	private IndexObserver observer;
	
	public LoginInternalFrame(IndexObserver observer) {
		/*super(Title, Resizable, Closable, Maximizable, Iconifiable)*/
		super("Login", false, false, false, true);
		setSize (widthThis, heightThis);
		
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension jInternalFrameSize = this.getSize();
		setLocation((dim.width - jInternalFrameSize.width)/2, ((dim.height- jInternalFrameSize.height)/2)-100);
		
		JPanel panel = panel();
		getContentPane().add(panel);
		
		this.observer = observer;
		
		setVisible (true);
	}

	private JPanel panel(){
		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		JLabel loginLbl = new JLabel("Login");
		loginLbl.setFont(new Font("Tahoma", Font.BOLD, 20));
		loginLbl.setBounds(10, 5, 70, 28);
		panel.add(loginLbl);
		
		JLabel descLbl = new JLabel("Masukkan username dan password Anda");
		descLbl.setFont(new Font("Tahoma", Font.PLAIN, 12));
		descLbl.setBounds(10, 30, 230, 18);
		panel.add(descLbl);
		
		JPanel headerPanel = new JPanel();
		headerPanel.setBounds(0, 0, this.getWidth(), 55);
		headerPanel.setBackground(Color.WHITE);
		headerPanel.setForeground(new Color(0, 0, 0, 65));
		headerPanel.setBorder(new MatteBorder(0, 0, 1, 0, (Color) SystemColor.scrollbar));
		panel.add(headerPanel);
		
		JLabel usernameLbl = new JLabel("Username");
		usernameLbl.setFont(new Font("Tahoma", Font.BOLD, 15));
		usernameLbl.setBounds(32, 75, 100, 28);
		panel.add(usernameLbl);
		
		usernameField = new JTextField();
		usernameField.setBounds(155, 75, 203, 28);
		usernameField.setColumns(10);
		panel.add(usernameField);
		
		JLabel passLbl = new JLabel("Password");
		passLbl.setFont(new Font("Tahoma", Font.BOLD, 15));
		passLbl.setBounds(32, 115, 100, 28);
		panel.add(passLbl);
		
		passField = new JPasswordField();
		passField.setBounds(155, 115, 203, 28);
		passField.setColumns(10);
		panel.add(passField);
		
		JPanel formPanel = new JPanel();
		formPanel.setBorder(new LineBorder(new Color(192, 192, 192)));
		formPanel.setBounds(10, 59, 370, 110);
		panel.add(formPanel);
		
		JSeparator separator = new JSeparator();
		separator.setForeground(new Color(192, 192, 192));
		separator.setBackground(new Color(192, 192, 192));
		separator.setBounds(10, 174, 370, 1);
		panel.add(separator);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(302, 184, 78, 25);
		btnSubmit.addActionListener(this);
		this.getRootPane().setDefaultButton(btnSubmit);
		panel.add(btnSubmit);
		
		return panel;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj==btnSubmit){
			String _username = usernameField.getText();
			String _pass = passField.getText();
			
			if(_username.equalsIgnoreCase("")){
				JOptionPane.showMessageDialog(null, "Silahkan masukkan username Anda", "EMPTY", JOptionPane.ERROR_MESSAGE);
				usernameField.requestFocus();
				return;
			}
			
			if(_pass.equalsIgnoreCase("")){
				JOptionPane.showMessageDialog(null, "Silahkan masukkan password Anda", "EMPTY", JOptionPane.ERROR_MESSAGE);
				passField.requestFocus();
				return;
			}
			

			UserDao loginQuery = new UserDao();
			User user = loginQuery.login(_username, _pass);
			if(user==null){
				JOptionPane.showMessageDialog(null, "User tidak ditemukan", "NOT FOUND", JOptionPane.ERROR_MESSAGE);
				usernameField.setText("");
				passField.setText("");
				return;
			}
			
			
			if(!user.getPassword().equalsIgnoreCase(_pass)){
				JOptionPane.showMessageDialog(null, "Password salah", "Incorrect", JOptionPane.ERROR_MESSAGE);
				passField.setText("");
				return;
			}
			
			
			LoginInternalFrame.this.setVisible(false);
			LoginInternalFrame.this.dispose();
			
			observer.loginResult(true, user);
			
		}
		
	}
	
	
	

}
